import { defineStore } from "pinia";
import { Operate, Params, Person, InsertPerson, PersonDetail, ReceivePerson, ExtendPerson, RejectPerson, IsolatePerson, BatchInsertPerson } from '@/types/person'
import { getPersonList, insertPerson, getPersonInfo, updatePerson, receivePerson, rejectPerson, extendPerson, relievePerson, isolatePerson, batchInsertPerson } from '@/api/person'
import { Pagination } from '@/types/global'

export const usePersonStore = defineStore({
  id: 'person',
  persist: true,
  state() {
    return {
      showAcceptDialog: false,
      showReleaseDialog: false,
      showRejectedDialog: false,
      showExtendDialog: false,
      showHouseholdDialog: false,
      showRelieveDialog: false,
      operateList: [
        { label: '确认接收', fn: 'setShowAcceptDialog', confirm_status: 1},
        { label: '驳回请求', fn: 'setShowRejectedDialog', confirm_status: 1},
        { label: '延长隔离', fn: 'setShowExtendDialog', isolate_status: 1},
        { label: '居家隔离', fn: 'setShowHouseholdDialog', isolate_status: 2},
        { label: '解除隔离', fn: 'setShowRelieveDialog', isolate_status: 1},
      ] as Operate[],
      pageState:{
        personList: [] as Person[],
        finished: false,
        current_page: 1
      },
      isolate_status: [
        { id: '1', label: '隔离中' },
        { id: '2', label: '解除隔离' },
      ],
      isolate_status_obj: {
        1: '隔离中', 2: '解除隔离'
      },
      confirm_status: [
        { id: '1', label: '待确认' },
        { id: '2', label: '已确认' },
        { id: '3', label: '驳回' },
      ],
      confirm_status_obj: {
        1: '待确认', 2: '已确认', 3: '驳回'
      },
      sexList: [
        { id: '1', label: '男' },
        { id: '2', label: '女' },
      ],
      sex_obj: {
        1: '男', 2: '女'
      },
      personInfo: {} as PersonDetail,
      pagination: {} as Pagination
    };
  },
  actions: {
    setShowAcceptDialog(data:boolean) {
      this.showAcceptDialog = data
    },
    setShowReleaseDialog(data:boolean) {
      this.showReleaseDialog = data
    },
    setShowRejectedDialog(data:boolean) {
      this.showRejectedDialog = data
    },
    setShowExtendDialog(data:boolean) {
      this.showExtendDialog = data
    },
    setShowHouseholdDialog(data:boolean) {
      this.showHouseholdDialog = data
    },
    setShowRelieveDialog(data:boolean) {
      this.showRelieveDialog = data
    },
    getPersonList(payload: Params) {
      return new Promise((resolve, reject) => {
        getPersonList(payload)
          .then(res => {
            if(res.code === 200) {
              if(payload.page === 1) {
                this.pageState.personList = (res.data as any).data
              } else {
                this.pageState.personList = this.pageState.personList.concat((res.data as any).data)
              }

              const pagination = (res.data as any).meta.pagination
              this.pageState.current_page = pagination.current_page
              if(pagination.current_page === pagination.total_pages) {
                this.pageState.finished = true
              } else {
                this.pageState.finished = false
              }
              this.pagination = pagination
            }
            resolve(res.data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    insertPerson(payload: InsertPerson) {
      return new Promise((resolve, reject) => {
        insertPerson(payload)
          .then(res => {
            resolve(res.code)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getPersonInfo(payload: string) {
      return new Promise((resolve, reject) => {
        getPersonInfo(payload)
          .then(res => {
            if(res.code === 200) {
              this.personInfo = res.data as PersonDetail
            }
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    updatePerson(payload: PersonDetail, id:string) {
      return new Promise((resolve, reject) => {
        updatePerson(payload, id)
          .then(res => {
            resolve(res.code)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    receivePerson(payload: ReceivePerson, id:string) {
      return new Promise((resolve, reject) => {
        receivePerson(payload, id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    rejectPerson(payload: RejectPerson, id:string) {
      return new Promise((resolve, reject) => {
        rejectPerson(payload, id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    extendPerson(payload: ExtendPerson, id:string) {
      return new Promise((resolve, reject) => {
        extendPerson(payload, id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    relievePerson(id:string) {
      return new Promise((resolve, reject) => {
        relievePerson(id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    isolatePerson(payload: IsolatePerson, id:string) {
      return new Promise((resolve, reject) => {
        isolatePerson(payload,id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    batchInsertPerson(payload: BatchInsertPerson) {
      return new Promise((resolve, reject) => {
        batchInsertPerson(payload)
          .then(res => {
            resolve(res.code)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
  }
})